from django.test import TestCase

# 引入模型类模块
from .models import NewsCategory, NewsInfo

# 1.插入数据
# 创建新闻分类对象
news_cag = NewsCategory()
news_cag.cag_name = '军事新闻'
news_cag.save()
print(NewsCategory.objects.all())
# 创建新闻信息对象
news_info = NewsInfo()
news_info.news_title = "大战一触即发"
news_info.news_content = "这是一个假新闻"
news_info.news_category = news_cag
news_info.save()
print(NewsInfo.objects.all())

# # 2.修改数据
# # 获取数据并且修改
# print(NewsInfo.objects.all())
# news_info = NewsInfo.objects.get(id=1)
# news_info.news_title = "国际新闻"
# news_info.save()
# print(NewsInfo.objects.all())


# # 3.查询数据
# # 查询所有数据
# news_info = NewsInfo.objects.all()
# for index, item in enumerate(news_info):
#     print("%s 新闻标题: %s, 新闻内容：%s, 新闻分类id: %s" % (index, item.news_title, item.news_content, item.news_category))
# item = NewsInfo.objects.get(pk=1)
# print("新闻标题: %s, 新闻内容：%s, 新闻分类id: %s" % (item.news_title, item.news_content, item.news_category))


# # 4.删除数据
# news_info = NewsInfo.objects.get(pk=1)
# news_info.delete()
# print(NewsInfo.objects.all())
